package com.cuiweiyou.bluemesh.util;

import com.cuiweiyou.bluemesh.bluetooth.BluetoothMaster;
import com.cuiweiyou.bluemesh.model.LightModel;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class ProcessUtil {

    private ProcessUtil() {
    }

    public static void processBlueGroup(final OnProcessBlueGroupFinishListener listener) {
        Observable//
                .create(new ObservableOnSubscribe<LightModel>() {
                    @Override
                    public void subscribe(ObservableEmitter<LightModel> emitter) throws Exception {
                        Map<String, List<LightModel>> deviceMap = BluetoothMaster.getInstance().getDeviceMap();
                        Iterator<String> iterator = deviceMap.keySet().iterator();
                        while (iterator.hasNext()) {
                            String group = iterator.next();
                            List<LightModel> lightModelList = deviceMap.get(group);
                            for (int i = 0; i < lightModelList.size(); i++) {
                                emitter.onNext(lightModelList.get(i));
                            }
                        }

                        emitter.onComplete();
                    }
                })//
                .flatMap(new Function<LightModel, ObservableSource<LightModel>>() {
                    @Override
                    public ObservableSource<LightModel> apply(LightModel light) throws Exception {
                        String nickname = light.getNickname();
                        NicknameSqlUtil.getInstance().insert(light, nickname); // 初始化设置完毕
                        return Observable.just(light);
                    }
                })//
                .toList()//
                .toObservable()//
                .subscribeOn(Schedulers.newThread())//
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(new Consumer<List<LightModel>>() {
                    @Override
                    public void accept(List<LightModel> lightModels) throws Exception {
                        if (null != listener) {
                            listener.onProcessBlueGroupFinish(true);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (null != listener) {
                            listener.onProcessBlueGroupFinish(false);
                        }
                    }
                });
    }

    public interface OnProcessBlueGroupFinishListener {
        public void onProcessBlueGroupFinish(boolean success);
    }
}
